package com.bluegalaxy.nhb.snapi.core.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.nhb.util.StringUtil;

//处理查询参数
public class UntisCom {

    //拼装sql查询where后面查询条件
    public static String aflerWhere(JSONArray jsonArray ,JSONArray applicationCodeArray ){
        StringBuilder aflerSql=new StringBuilder();
        return aflerSql.append(getWsql(jsonArray).append(applicationCode(applicationCodeArray))).toString();
    }
    //拼装sql查询where前面部分
    public static String beforeWhere (String methodName ,String fieldName ){
        String beforeSql=" count   (1)  eventCount ";
        if(methodName!=null){
            beforeSql=getSsql(methodName,fieldName);
        }
        return beforeSql;
    }
    public static String beforeFloatWhere (String methodName ,String fieldName ){
        String beforeSql=" toFloat32(count   (1))  eventCount ";
        if(methodName!=null){
            beforeSql=getSsql(methodName,fieldName);
        }
        return beforeSql;
    }
    public static String beforeWhere (String methodName){
        String beforeSql=" count   (1)  eventCount ";
        if("sum".equals(methodName)){
            beforeSql=" COUNT(DISTINCT e.distinct_id)  eventCount ";
        }else if ("avg".equals(methodName)){
            beforeSql=" intDivOrZero(count(1),COUNT(DISTINCT e.distinct_id))  eventCount ";
        }
        return beforeSql;
    }

    private static String applicationCode(JSONArray applicationCodeArray){
        StringBuilder pSql=new StringBuilder();
        if(applicationCodeArray!=null&&applicationCodeArray.size()>0){
            for(int i=0;i<applicationCodeArray.size();i++) {
                String outJson = String.valueOf(applicationCodeArray.get(i));
                StringBuilder str=new StringBuilder(" ");
                if(i>=1){
                    str.append(" or ");
                }
                str.append(" application_ode= ").append("'"+outJson+"'");
                pSql.append(str);
            }
            StringBuilder builder=new StringBuilder(" and ( ");
            return  builder.append(pSql).append("   ) ").toString();
        }
       return " ";

    }

    private  static String less(String n){
        String less=" > '";
        if("less".equals(n)){
            less= " < '";
        }else if("gatter".equals(n)){
            less= " > '";
        }else if("equal".equals(n)){
            less= " = '";
        }
        return  less;
    }

    public static String getSsql(String methodName,String fieldName){
        return " " +methodName+"(toFloat32(if(e."+fieldName+"=='','0',e."+fieldName+")))   eventCount ";
    }

    public static String getSsqlAmount(String methodName,String fieldName){
        return " " +methodName+"(toFloat32(if(e."+fieldName+"=='','0',e."+fieldName+")))   eventAmount ";
    }


    private static StringBuilder getWsql(JSONArray jsonArray){
        StringBuilder pSql=new StringBuilder();
        if(jsonArray!=null&&jsonArray.size()>0){
            for(int i=0;i<jsonArray.size();i++) {
                JSONObject outJson = jsonArray.getJSONObject(i);
                if(outJson!=null){
                    StringBuilder str=new StringBuilder("and");
                    if("between".equals(outJson.getString("eventItemExpression"))){
//                        str.append("'  t." + outJson.getString("eventItemCode")+ " between '" + ru[4] + "' and '" + ru[5] +"' ) " );
                    }else{
                        str.append(" e."+outJson.getString("eventItemCode") +less(outJson.getString("eventItemExpression"))+outJson.getString("value1Str") +"'  " );
                    }
                    pSql.append(str);
                }
            }
        }
        return  pSql;
    }

    public static String getGroupBySql(String groupBy){
        String gSql = " (LEFT(e.`time`,10)) as eventGroup ";
        if (StringUtil.isNotEmpty(groupBy)){
            if (!"time".equals(groupBy)){
                return " case when "+groupBy+" is null or "+groupBy+" = '' then '未知' else "+groupBy+" end as eventGroup ";
            }
        }
        return gSql;
    }
}
